﻿using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Application.Base;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Entities;
using Dora.Services.AdminUser.IApplication;
using Dora.Services.AdminUser.Repository.Repositorys;
using Dora.Tools.Utility.Const;
using Microsoft.Extensions.Logging;
using Surging.Core.Common;
using Surging.Core.CPlatform.Ioc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Application
{
    /// <summary>
    /// 广告位表服务
    /// </summary>
    [ModuleName(DoraModuleName.AdminUser)]
    public class AdPositionService : DoraServiceBase, IAdPositionService
    {

        private ISugarUnitOfWork _unitOfWork;
        private readonly AdPositionRepository _repository;
        public AdPositionService(ISugarUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _repository = new AdPositionRepository(unitOfWork);
        }


        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultDto<int>> Create(AdPositionEntity entity)
        {
            entity.CreatedTime = DateTime.Now;
            var MaxId = await _repository.GetMaxId();
            if (MaxId > 0)
            {
                MaxId++;
                entity.Id = MaxId;
            }
            var result = await _repository.InsertAsync(entity);
            return ResultDto<int>.Success(result.Id);
        }


        /// <summary>
        /// 根据Id获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdPositionEntity> GetById(int id)
        {
            return await _repository.FirstOrDefaultAsync(id);
        }


        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<AdPositionEntity>> GetPageList(AdPositionQueryDto input)
        {
            var page = await _repository.GetPageList(input);
            return page;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> Update(AdPositionEntity entity)
        {
            entity.UpdatedTime = DateTime.Now;
            return await _repository.UpdateAsync(entity);
        }

        /// <summary>
        /// 根据IdList获取列表
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public async Task<List<AdPositionEntity>> GetList(List<int> idList)
        {
            if (idList == null || idList.Count == 0)
            {
                return new List<AdPositionEntity>();
            }
            var list = await _repository.GetAllListAsync(it => idList.Contains(it.Id));
            return list;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<AdPositionEntity>> GetAll()
        {
            var list = await _repository.GetAllListAsync();
            return list;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> Delete(AdPositionEntity entity)
        {
            var result = await _repository.Delete(entity);
            return ResultDto<bool>.Success(result);
        }
    }
}
